import { baseRequestClient, requestClient } from '#/api/request';

export namespace SettlementApi {
  /** 结算单列表查询参数 */
  export interface SettlementListParams {
    page?: number;
    per_page?: number;
    settlement_id?: string;
    settlement_name?: string;
    task_id?: number;
    status?: 'pending_submit' | 'distributing' | 'completed' | 'cancelled' | 'distribute_failed' | 'partial_failed';
    start_date?: string;
    end_date?: string;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
  }

  /** 结算单记录接口 */
  export interface SettlementRecord {
    id: string;
    settlement_number: string;
    settlement_name: string;
    tenant_id: number;
    task_id?: number;
    task_name?: string;
    landing_company: string;
    total_amount: number;
    total_required_balance?: number;
    totalRequiredBalance?: number;
    success_amount: number;
    total_count: number;
    success_count: number;
    status: 'pending_submit' | 'distributing' | 'completed' | 'cancelled' | 'distribute_failed' | 'partial_failed';
    status_display: string;
    deliverables?: string;
    remarks?: string;
    uploaded_files?: any[];
    success_rate: number;
    amount_success_rate: number;
    created_at: string;
    updated_at: string;
    submitted_at?: string;
    distribute_started_at?: string;
    completed_at?: string;
  }

  /** 任务选项接口 */
  export interface TaskOption {
    id: number;
    name: string;
  }

  /** 分页响应结构 */
  export interface SettlementListResponse {
    data: SettlementRecord[];
    current_page: number;
    per_page: number;
    total: number;
    last_page: number;
    from?: number;
    to?: number;
    first_page_url?: string;
    last_page_url?: string;
    next_page_url?: string;
    prev_page_url?: string;
    path?: string;
    links?: any[];
  }

  /** API响应包装 */
  export interface ApiResponse<T = any> {
    success: boolean;
    data: T;
    message?: string;
  }

  export interface LandingCompanyOption {
    value: string | number;
    label: string;
    name?: string;
    code?: string;
  }

  export interface CreateSettlementParams {
    task_id: string | number;
    settlement_name: string;
    landing_company: string | number;
    settlement_channel?: string;
    remarks?: string;
  }
}

/**
 * 获取结算单列表
 */
export async function getSettlementListApi(params: SettlementApi.SettlementListParams) {
  return requestClient.get<SettlementApi.SettlementListResponse>('/api/tenant/settlements', {
    params,
  });
}

/**
 * 获取任务选项 (用于下拉选择框)
 */
export async function getTaskOptionsApi() {
  return requestClient.get<SettlementApi.TaskOption[]>('/api/tenant/tasks/options');
}

/**
 * 获取结算单详情
 */
export async function getSettlementDetailApi(id: string) {
  return requestClient.get<SettlementApi.SettlementRecord>(`/api/tenant/settlements/${id}`);
}

export async function batchPaySettlementApi(settlementNumber: string) {
  return requestClient.post<null>(`/api/tenant/settlements/${settlementNumber}/batch-pay`);
}

export async function triggerSettlementTaxApi(settlementId: string) {
  return requestClient.post<SettlementApi.ApiResponse<null>>(
    `/api/tenant/settlements/${settlementId}/trigger-tax`,
  );
}

/**
 * 创建结算单
 */
export async function createSettlementApi(
  data: FormData | Partial<SettlementApi.CreateSettlementParams>,
) {
  const isFormData = typeof FormData !== 'undefined' && data instanceof FormData;
  return requestClient.post<SettlementApi.SettlementRecord>(
    '/api/tenant/settlements',
    data,
    isFormData
      ? {
          headers: {
            'Content-Type': 'multipart/form-data',
          },
        }
      : undefined,
  );
}

/**
 * 更新结算单
 */
export async function updateSettlementApi(id: string, data: Partial<SettlementApi.SettlementRecord>) {
  return requestClient.put<SettlementApi.ApiResponse<SettlementApi.SettlementRecord>>(`/api/tenant/settlements/${id}`, data);
}

/**
 * 删除结算单
 */
export async function deleteSettlementApi(id: string) {
  return requestClient.delete<SettlementApi.ApiResponse<null>>(`/api/tenant/settlements/${id}`);
}

/**
 * 提交结算单
 */
export async function submitSettlementApi(id: string) {
  return requestClient.post<SettlementApi.ApiResponse<SettlementApi.SettlementRecord>>(`/api/tenant/settlements/${id}/submit`);
}

/**
 * 取消结算单
 */
export async function cancelSettlementApi(id: string) {
  return requestClient.post<SettlementApi.ApiResponse<SettlementApi.SettlementRecord>>(`/api/tenant/settlements/${id}/cancel`);
}

/**
 * 批量操作
 */
export async function batchActionSettlementApi(data: { action: string; ids: string[] }) {
  return requestClient.post<SettlementApi.ApiResponse<null>>('/api/tenant/settlements/batch-action', data);
}

/**
 * 获取落地公司选项
 */
export async function getSettlementLandingCompanyOptionsApi() {
  return requestClient.get<SettlementApi.LandingCompanyOption[]>('/api/tenant/landing-companies/options');
}
